//
//  MineHeaderView.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/13.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "MineHeaderView.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"

@interface MineHeaderView()
@property (nonatomic,weak) UILabel *nameLabel;
@end
@implementation MineHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
//    UIImage *image = [UIImage imageNamed:@"MineHeaderBackImg"];
//    self.layer.contents = (id)image.CGImage;
    UIImageView *backImgView = [[UIImageView alloc] initWithFrame:self.frame];
    backImgView.image = [UIImage imageNamed:@"MineHeaderBackImg"];
    backImgView.contentMode = UIViewContentModeScaleAspectFill;
    backImgView.clipsToBounds = YES;
    backImgView.userInteractionEnabled = YES;
    _backImgView = backImgView;
    [self addSubview:self.backImgView];
    
    UIButton *imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect imgFrame = CGRectMake(0, 0, 180*SCALEUNIT, 180*SCALEUNIT);
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:imgFrame byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(90*SCALEUNIT, 90*SCALEUNIT)];
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.frame = imgFrame;
    shapeLayer.path = bezierPath.CGPath;
    imgBtn.layer.mask = shapeLayer;
    _imgBtn = imgBtn;
    [self addSubview:self.imgBtn];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:LoginFont+3];
    _nameLabel = nameLabel;
    [self addSubview:self.nameLabel];
    //布局
    WS(ws);
    [self.imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws).with.offset(95*SCALEUNIT);
        make.centerX.mas_equalTo(ws);
        make.size.mas_equalTo(CGSizeMake(180*SCALEUNIT, 180*SCALEUNIT));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.imgBtn.mas_bottom).with.offset(10*SCALEUNIT);
        make.centerX.mas_equalTo(ws);
        make.size.mas_equalTo(CGSizeMake(300*SCALEUNIT, 60*SCALEUNIT));
    }];
}

#pragma mark ==========数据填充==========
- (void)setModel:(UserModel *)model{
    _model = model;
    if ([_model.imgPath isEqualToString:@""]) {
        [self.imgBtn setBackgroundImage:[UIImage imageNamed:[UserMessage sharedInstance].model.gender==0?@"MineGirlImage":@"MineBoyImage"] forState:UIControlStateNormal];
    } else {
        [self.imgBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:_model.imgPath] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:[UserMessage sharedInstance].model.gender==0?@"MineGirlImage":@"MineBoyImage"]];
    }
     self.nameLabel.text = _model.name;
}


@end
